﻿using DatabaseTransfer.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseTransfer.Utils
{
    [Serializable()] 
    public class ColumnLinkage : ISerializable
    {
        public TableColumn SourceColumn { get; private set; }
        public TableColumn DestinationColumn { get; private set; }
        public string Conversion { get; set; }
        public string DisplayLinkage
        {
            get
            {
                return this.ToString();
            }
        }

        public ColumnLinkage(TableColumn srcColumn, TableColumn destCol) : this(srcColumn, destCol, null) { }

        public ColumnLinkage(TableColumn destCol, string conversion) : this(null, destCol, conversion) { }
        
        public ColumnLinkage(TableColumn srcColumn, TableColumn destCol, String conversion)
        {
            this.SourceColumn = srcColumn;
            this.DestinationColumn = destCol;
            this.Conversion = conversion;
        }

        public ColumnLinkage(SerializationInfo info, StreamingContext ctxt)
        {
            this.SourceColumn = (TableColumn)info.GetValue("SourceColumn", typeof(TableColumn));
            this.DestinationColumn = (TableColumn)info.GetValue("DestinationColumn", typeof(TableColumn));
            this.Conversion = (string)info.GetValue("Conversion", typeof(string));
        }

        public override string ToString()
        {
            if (SourceColumn != null && DestinationColumn != null)
            {
                return String.Format("{0} -> {1}", this.SourceColumn.ColumnName, this.DestinationColumn.ColumnName);
            }

            if (DestinationColumn != null && Conversion != null)
            {
                return String.Format("{0} -> {1}", this.DestinationColumn.ColumnName, this.Conversion);
            }

            return base.ToString();
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("SourceColumn", this.SourceColumn);
            info.AddValue("DestinationColumn", this.DestinationColumn);
            info.AddValue("Conversion", this.Conversion);
        }
    }
}
